package com.teasale.controller;

import com.alibaba.fastjson.JSONObject;
import com.teasale.entity.Order;
import com.teasale.vo.OrderGoodsOV;
import com.teasale.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class OrdersController {
    @Autowired
    private OrderService orderService;

    /**
     获取所有订单列表
     **/
    @GetMapping("api/orders/getAllOrdersDes")
    public List<OrderGoodsOV> selectAllOrder(@RequestParam String id){
        return orderService.selectAllOrder(id);
    }


    /**
     获取所有订单
     **/
    @GetMapping("api/orders/getAllOrders")
    public List<Order> selectAllOrders(){
        return orderService.query().list();
    }

    /**
    根据用户id获取订单列表
     **/
    @GetMapping("api/orders/getOneById")
    public List<OrderGoodsOV> getAll(@RequestParam Integer id){
        List<OrderGoodsOV> orderGoodsOVS = orderService.find(id);
        return orderGoodsOVS;
    }


    /**
     根据订单id删除单个商品
     **/
    @PostMapping("api/orders/delOrder")
    public String delOrder(@RequestBody Map<String, Integer> param){
        Integer orderId = param.get("orderId");
        orderService.delOrder(orderId);
        return "ok";
    }

    /**
     更新订单状态
     **/
    @PostMapping("api/orders/updateOrderPayStatus")
    public String updateOrderPayStatus(@RequestBody Map<String, Integer> param){
        Integer orderId = param.get("orderId");
        orderService.updateOrderPayStatus(orderId, 1);
        return "ok";
    }


    /**
     * 新增订单
     * @param data
     * @return
     */
    @PostMapping(value = "api/addOrders")
    @ResponseBody
    public String addOrders(@RequestBody JSONObject data ){
        Integer user_id = (Integer) data.get("user_id");
        List<String> good_id = (List<String>) data.get("good_id");
        String[] goods_id = new String[good_id.size()];
        for(int i = 0; i < good_id.size();i++){
            goods_id[i] = good_id.get(i);
        }
        double orderPrice = data.getFloat("order_price");
        double og_price = data.getFloat("og_price");
        List<Integer> og_num = (List<Integer>) data.get("og_num");
        int[] ogs_num = new int[og_num.size()];
        for(int i = 0; i < good_id.size();i++){
            ogs_num[i] = og_num.get(i);
        }
        return orderService.addOrders(user_id, goods_id, orderPrice, og_price,ogs_num);
    }

}
